import QtQuick 1.1
import com.nokia.meego 1.0
import "Logic.js" as Logic
import cppWiFiGraph 1.0

Page {
    id: pageNetworkMonitor
    anchors.margins: 3
    orientationLock: appWindow.lockOrientation()

    tools: ToolBarLayout {
        visible: true
        ToolIcon { iconId: "toolbar-back"; onClicked: { pageStack.pop(); } }
        ToolIcon {
            id: monitorStartStopButton
            iconId: "toolbar-mediacontrol-play"
            smooth: true
            onClicked: {
                startWiFiMonitor()
            }
        }
    }

    /* запуск/остановка автоматического обновления данных */
    function startWiFiMonitor( ) {
        if ( !wifiGraph.isWork ) {
            monitorStartStopButton.iconId = "toolbar-mediacontrol-stop"
            wifiGraph.timerStart()
        } else {
            monitorStartStopButton.iconId = "toolbar-mediacontrol-play"
            wifiGraph.timerStop()
        }
    }

    BackgroundPage { }

    Rectangle {
        id: itemIndicator
        anchors.top: parent.top
        anchors.left: parent.left
        height: screen.orientationString == "Landscape" || screen.orientationString == "LandscapeInverted" ? parent.height : parent.height/2 + 30
        width: screen.orientationString == "Landscape" || screen.orientationString == "LandscapeInverted" ? parent.width/2 : parent.width
        border.color: "blue"
        radius: 10
        color: "black"

        Rectangle {
            id: wrapperGraph
            anchors.top: parent.top
            anchors.topMargin: 3
            anchors.left: parent.left
            anchors.leftMargin: 1
            height: parent.height/2
            width:  parent.width - 2
            color: "black"
            clip: true
            WiFiGraph {
                id: wifiGraph
                height: parent.height
                width: parent.width
                identify: pageWiFiMonitor.indicatorMac
                onChangeValueParam: {
                    //console.log( "***onChangeValueParam: val_per=" + wifiGraph.val_per )
                    //console.log( "***onChangeValueParam: val_dbm=" + wifiGraph.val_dbm )
                    //objIndicator.value = wifiGraph.val_per * 100
                    if( ((wifiGraph.val_dbm <100)&&(wifiGraph.val_dbm >0) ) || ((wifiGraph.val_dbm !== 0) && (wifiGraph.val_per !== 0)) ) {
                        imgArrow.value = wifiGraph.val_dbm
                        lblLevelVal.value = wifiGraph.val_dbm
                        lblQualityVal.value = wifiGraph.val_per*100
                    } else {
                        imgArrow.value = 100.0
                        lblLevelVal.value = 100
                        lblQualityVal.value = 0
                    }
                }
            }
        }

        Image {
            id: imgScale
            source: "pictures/indicator.png"
            anchors.top: wrapperGraph.bottom
            anchors.horizontalCenter: parent.horizontalCenter
            clip: true
            Image {
                id: imgArrow
                source: "pictures/arrow.png"
                property real value: 100.0
                rotation: (57 - imgArrow.value*2*57/100.0)
                Behavior on rotation { SpringAnimation{ spring: 1.4; damping: 0.15 } }
                smooth: true
                anchors.bottom: parent.bottom
                //anchors.bottomMargin: 20
                anchors.bottomMargin: 5
                anchors.horizontalCenter: parent.horizontalCenter
                transformOrigin: Item.Bottom
            }
        }
    }

    property int _sizFont: 30

    Rectangle {
        id: itemDescript
        anchors.top: screen.orientationString == "Landscape" || screen.orientationString == "LandscapeInverted" ? parent.top : itemIndicator.bottom
        anchors.left: screen.orientationString == "Landscape" || screen.orientationString == "LandscapeInverted" ? itemIndicator.right : parent.left
        height: screen.orientationString == "Landscape" || screen.orientationString == "LandscapeInverted" ? parent.height : parent.height/2 - 30
        width: screen.orientationString == "Landscape" || screen.orientationString == "LandscapeInverted" ? parent.width/2 : parent.width
        border.color: "blue"
        radius: 10
        color: "transparent"

        Item {
            id: infoPane1
            anchors.top: parent.top
            anchors.left: parent.left
            width: parent.width
            height: 100
            Image {
                id: imgWiFiNet
                anchors.left: parent.left
                anchors.leftMargin: 20
                anchors.verticalCenter: parent.verticalCenter
                source: pageWiFiMonitor.indicatorPic
            }
            Label {
                id: lblEssid
                font.pixelSize: _sizFont
                anchors.left: imgWiFiNet.right
                anchors.leftMargin: 20
                anchors.top: parent.top
                anchors.topMargin: 10
                text: pageWiFiMonitor.indicatorEssid
                color: mytheme.fontAltColor
                width: parent.width - imgWiFiNet.width - 20 - 20 - 15
                height: _sizFont + 5
                clip: true
            }
            Label {
                id: lblMac
                anchors.bottom: parent.bottom
                anchors.bottomMargin: 12
                font.pixelSize: _sizFont-7
                anchors.left: imgWiFiNet.right
                anchors.leftMargin: 20
                text: pageWiFiMonitor.indicatorMac
                color: mytheme.fontMainColor
            }
        }

        Grid {
            anchors.top: infoPane1.bottom
            anchors.left: parent.left
            width: parent.width
            height: parent.height - infoPane1.height
            //anchors.bottom: parent.bottom
            //anchors.left: screen.orientationString == "Landscape" || screen.orientationString == "LandscapeInverted" ? itemIndicator.right : parent.left
            //anchors.right:  parent.right
            columns: 2

            Column {
                height: parent.height/2
                width: parent.width/2
                Label {
                    font.pixelSize: _sizFont - 10
                    anchors.horizontalCenter: parent.horizontalCenter
                    text: qsTr("Level (dBm)")
                    color: mytheme.fontMainColor
                }
                Image {
                    source: "pictures/button_frame.png"
                    anchors.horizontalCenter: parent.horizontalCenter
                    height: 0.7*parent.height
                    width: height
                    smooth: true
                    Label {
                        id: lblLevelVal
                        property int value: 0
                        anchors.centerIn: parent
                        font.pixelSize: screen.orientationString == "Landscape" || screen.orientationString == "LandscapeInverted" ? _sizFont - 2 : _sizFont - 6
                        text:  "-" + lblLevelVal.value
                        color: mytheme.fontAltColor
                    }
                }
            }

            Column {
                height: parent.height/2
                width: parent.width/2
                Label {
                    font.pixelSize: _sizFont -10
                    anchors.horizontalCenter: parent.horizontalCenter
                    text: qsTr("Quality (%)")
                    color: mytheme.fontMainColor
                }
                Image {
                    source: "pictures/button_frame.png"
                    anchors.horizontalCenter: parent.horizontalCenter
                    height: 0.7*parent.height
                    width: height
                    smooth: true
                    Label {
                        id: lblQualityVal
                        property int value: 0
                        anchors.centerIn: parent
                        font.pixelSize: screen.orientationString == "Landscape" || screen.orientationString == "LandscapeInverted" ? _sizFont - 2 : _sizFont - 6
                        text: value
                        color: mytheme.fontAltColor
                    }
                }
            }

            Column {
                height: parent.height/2
                width: parent.width/2
                Label {
                    font.pixelSize: _sizFont - 10
                    anchors.horizontalCenter: parent.horizontalCenter
                    text: qsTr("Frequency (MHz)")
                    color: mytheme.fontMainColor
                }
                Image {
                    source: "pictures/button_frame.png"
                    anchors.horizontalCenter: parent.horizontalCenter
                    height: 0.7*parent.height
                    width: height
                    smooth: true
                    Label {
                        property int value: 0
                        anchors.centerIn: parent
                        font.pixelSize: screen.orientationString == "Landscape" || screen.orientationString == "LandscapeInverted" ? _sizFont - 2 : _sizFont - 6
                        text: pageWiFiMonitor.indicatorFreq*1000
                        color: mytheme.fontAltColor
                    }
                }
            }

            Column {
                height: parent.height/2
                width: parent.width/2
                Label {
                    font.pixelSize: _sizFont -10
                    anchors.horizontalCenter: parent.horizontalCenter
                    text: qsTr("Channel")
                    color: mytheme.fontMainColor
                }
                Image {
                    source: "pictures/button_frame.png"
                    anchors.horizontalCenter: parent.horizontalCenter
                    height: 0.7*parent.height
                    width: height
                    smooth: true
                    Label {
                        property int value: 0
                        anchors.centerIn: parent
                        font.pixelSize: screen.orientationString == "Landscape" || screen.orientationString == "LandscapeInverted" ? _sizFont - 2 : _sizFont - 6
                        text: pageWiFiMonitor.indicatorChannel
                        color: mytheme.fontAltColor
                    }
                }
            }

        }

    }
}
